package leetcode.D100.T34;

class Solution {
    // 一刷
    /*public int[] searchRange(int[] nums, int target) {
        int a = lowerBound(nums, target);
        if(a == nums.length || nums[a] != target) {
            return new int[]{-1, -1};
        }
        int b = upperBound(nums, target);
        return new int[]{a, b-1};
    }
    private int lowerBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while(left < right) {
            int mid = left + (right-left) / 2;
            if(nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
    private int upperBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while(left < right) {
            int mid = left + (right-left) / 2;
            if(nums[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }*/

    // 二刷
    /*public int[] searchRange(int[] nums, int target) {
        int a = lowerBound(nums, target);
        if(a == -1) {
            return new int[]{-1, -1};
        }
        int b = upperBound(nums, target);
        return new int[]{a, b-1};
    }

    private int lowerBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while(left < right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        if(left == nums.length || nums[left] != target) {
            return -1;
        } else {
            return left;
        }
    }

    private int upperBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while(left < right) {
            int mid = left + (right - left) / 2;
            if(nums[mid] <= target) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return right;
    }*/

    // 三刷
    /*public int[] searchRange(int[] nums, int target) {
        if (nums.length == 0)
            return new int[]{-1, -1};
        int a = lowerBound(nums, target);
        if (a == -1)
            return new int[]{-1, -1};
        return new int[] {a, upperBound(nums, target)};
    }

    private int lowerBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while(left < right) {
            int mid = left + (right-left)/2;
            if (nums[mid] < target)
                left = mid + 1;
            else if (nums[mid] >= target)
                right = mid;
        }
        return (left == nums.length || nums[left] != target) ? -1 : left;
    }

    private int upperBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while(left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] <= target)
                left = mid + 1;
            else
                right = mid;
        }
        return left-1;
    }*/

    // 四刷
    public int[] searchRange(int[] nums, int target) {
        int lower = lowerBound(nums, target);
        if (lower == nums.length || nums[lower] != target)
            return new int[]{-1, -1};
        int upper = upperBound(nums, target);
        return new int[]{lower, upper-1};
    }

    private int lowerBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target)
                left = mid + 1;
            else
                right = mid;
        }
        return left;
    }

    private int upperBound(int[] nums, int target) {
        int left = 0, right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] <= target)
                left = mid + 1;
            else
                right = mid;
        }
        return left;
    }
}
